package edu.gatech.oops.servlets;

import java.io.*;
import java.util.Calendar;
/**
 * This is the login helper class. It helps send the data back to the validate servlet so the servlet can check the validity of the login
 * 
 * @author pushkara
 *
 */
public class LoginHelper {
	
	/**
	 * This is the login method that helps pull data from the data.txt file
	 * Its checks if the login matches any of the usernames
	 * and then if it matches that particular usernames' password
	 * If so it sends the info to teh validate server
	 * If not, it doesn't send the information to the 
	 * @param username
	 * @param password
	 * @return
	 * @throws IOException
	 */
	public static String[] login(String username, String password) throws IOException {
		boolean good = false;
		String[] info = null;
		File file = new File("/home/pushkara/workspace/LabAssistan/Data.txt");
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = reader.readLine();
		while(line!=null && !good){
			String[] data = line.split(",");
			if (username.equals(data[0]) && password.equals(data[1])) {
				good = true;
				info = data;
			}
			line = reader.readLine();
		}
		if(!good)
		{
			File eFile = new File("/home/pushkara/workspace/LabAssistan/LogExceptions.txt");
			BufferedWriter eWriter = new BufferedWriter(new FileWriter(eFile, true));
			eWriter.write(Calendar.getInstance().getTime() + ": Improper log in.\n");
			eWriter.close();
		}
		return info;
	}
}
